﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LeanBoard.ServiceReference1;
using LeanBoard.Models;

namespace LeanBoard
{
    class DominoStatuses
    {
        public const string ScheduledStatusKey = "Scheduled";
        public const string AnalysisStatusKey = "Analysis";
        public const string ReadyForAnalysisReviewStatusKey = "Ready for analysis review";
        public const string AnalysisReview = "Analysis review";
        public const string ReadyForDevelopment = "Ready for development";
        public const string DevelopmentStatusKey = "Development";
        public const string DevelopmentDoneStatusKey = "Ready for Code Review";
        public const string ReviewStatusKey = "Code Review";
        public const string ReviewDoneStatusKey = "Ready for Approval";
        public const string TestingStatusKey = "Testing";
        public const string DoneStatusKey = "Done";
    }

    class CSSStatuses
    {
        public const string ScheduledStatusKey = "scheduled";
        public const string AnalysisStatusKey = "analysis";
        public const string AnalysisReadyForReviewStatusKey = "analysisReadyForReview";
        public const string AnalysisReviewStatusKey = "analysisReview";
        public const string ReadyForDevelopmentStatusKey = "readyForDevelopment";
        public const string DevelopmentStatusKey = "development";
        public const string DevelopmentDoneStatusKey = "readyForCodeReview";
        public const string ReviewStatusKey = "codeReview";
        public const string ReviewDoneStatusKey = "readyForApproval";
        public const string TestingStatusKey = "testing";
        public const string DoneStatusKey = "done";
    }

    public class DataProvider : LeanBoard.IDataProvider
    {
        public DominoActionResult MoveItem(Item item)
        {
            eMgrWSP eMgrWsp = new eMgrWSPClient();
            string strstatus = TranslateStatus(item.Status);
            SETSTATUSResponse setstatusResponse = eMgrWsp.SETSTATUS(new SETSTATUSRequest(item.Unid, strstatus));
            string result = string.IsNullOrEmpty(setstatusResponse.SETSTATUSReturn.STATUSRESULTREASON)
                                ? "Done."
                                : setstatusResponse.SETSTATUSReturn.STATUSRESULTREASON;
            string className = setstatusResponse.SETSTATUSReturn.STATUSRESULTVALUE ? "Success" : "Failure";


            return new DominoActionResult(){Status=className,Message = result};
        }

        public DominoActionResult Assign(string itemUnid, string name)
        {
            eMgrWSP eMgrWsp = new eMgrWSPClient();
            SETPERSONResponse setpersonResponse = eMgrWsp.SETPERSON(new SETPERSONRequest() { STRUNID = itemUnid, STRPERSON = name, STRFIELD = "TASKRESPONSIBLE" });
            return new DominoActionResult() {Status = setpersonResponse.SETPERSONReturn.ToString()};
        }

        public ItemsCollection GetItems(string team, string release)
        {
            eMgrWSP eMgrWSP = new eMgrWSPClient();
            var getdocumentsRequest = new GETDOCUMENTSRequest();
            getdocumentsRequest.STRRELEASE = release??string.Empty;
            getdocumentsRequest.STRTYPE = string.Empty;
            getdocumentsRequest.STRTEAM = team??string.Empty;
            GETDOCUMENTSResponse getdocumentsResponse = eMgrWSP.GETDOCUMENTS(getdocumentsRequest);

            WSPDOC[] document = getdocumentsResponse.GETDOCUMENTSReturn.DOCUMENT;

            //WSPDOC[] document = new WSPDOC[0];

            var itemsCollection = new ItemsCollection();

            Dictionary<string, IList<Item>> items = new Dictionary<string, IList<Item>>();

            if (document[0] != null)
            {
                foreach (WSPDOC wspdoc in document)
                {
                    Item item = new Item()
                                    {
                                        DevResponsible = wspdoc.TASKRESPONSIBLE,
                                        Id = wspdoc.NRDOC,
                                        QaResponsible = wspdoc.TASKRESPONSIBLE,
                                        Status = wspdoc.LEANSTATUS,
                                        Title = wspdoc.TITLE,
                                        Type = wspdoc.GLOBALTYPE,
                                        Unid = wspdoc.DOCID,
                                        Priority = wspdoc.PRIORITY,
                                        Team = wspdoc.TEAM,
                                        ItemType = wspdoc.DOCTYPE,
                                        Progress = wspdoc.PROGRESS
                                    };

                    if (!items.ContainsKey(item.Status))
                        items.Add(item.Status, new List<Item>());

                    items[item.Status].Add(item);
                }
            }

            itemsCollection.Scheduled = items.ContainsKey(DominoStatuses.ScheduledStatusKey) ? items[DominoStatuses.ScheduledStatusKey] : new List<Item>();
            itemsCollection.Analysis = items.ContainsKey(DominoStatuses.AnalysisStatusKey) ? items[DominoStatuses.AnalysisStatusKey] : new List<Item>();
            itemsCollection.AnalysisReadyForReview = items.ContainsKey(DominoStatuses.ReadyForAnalysisReviewStatusKey) ? items[DominoStatuses.ReadyForAnalysisReviewStatusKey] : new List<Item>();
            itemsCollection.AnalysisReview = items.ContainsKey(DominoStatuses.AnalysisReview) ? items[DominoStatuses.AnalysisReview] : new List<Item>();
            itemsCollection.ReadyForDevelopment = items.ContainsKey(DominoStatuses.ReadyForDevelopment) ? items[DominoStatuses.ReadyForDevelopment] : new List<Item>();
            itemsCollection.Development = items.ContainsKey(DominoStatuses.DevelopmentStatusKey) ? items[DominoStatuses.DevelopmentStatusKey] : new List<Item>();
            itemsCollection.ReadyForCodeReview = items.ContainsKey(DominoStatuses.DevelopmentDoneStatusKey) ? items[DominoStatuses.DevelopmentDoneStatusKey] : new List<Item>();
            itemsCollection.CodeReview = items.ContainsKey(DominoStatuses.ReviewStatusKey) ? items[DominoStatuses.ReviewStatusKey] : new List<Item>();
            itemsCollection.ReadyForApporval = items.ContainsKey(DominoStatuses.ReviewDoneStatusKey) ? items[DominoStatuses.ReviewDoneStatusKey] : new List<Item>();
            itemsCollection.Testing = items.ContainsKey(DominoStatuses.TestingStatusKey) ? items[DominoStatuses.TestingStatusKey] : new List<Item>();
            itemsCollection.Done = items.ContainsKey(DominoStatuses.DoneStatusKey) ? items[DominoStatuses.DoneStatusKey] : new List<Item>();

            itemsCollection.Scheduled = itemsCollection.Scheduled.OrderBy(x => x.Priority).ToArray();
            itemsCollection.ReadyForDevelopment = itemsCollection.ReadyForDevelopment.OrderBy(x => x.Priority).ToArray();
            return itemsCollection;
        }

        private static string TranslateStatus(string inputStatus)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();

            dict.Add(CSSStatuses.ScheduledStatusKey, DominoStatuses.ScheduledStatusKey);
            dict.Add(CSSStatuses.AnalysisStatusKey, DominoStatuses.AnalysisStatusKey);
            dict.Add(CSSStatuses.AnalysisReadyForReviewStatusKey, DominoStatuses.ReadyForAnalysisReviewStatusKey);
            dict.Add(CSSStatuses.AnalysisReviewStatusKey, DominoStatuses.AnalysisReview);
            dict.Add(CSSStatuses.ReadyForDevelopmentStatusKey, DominoStatuses.ReadyForDevelopment);
            dict.Add(CSSStatuses.DevelopmentStatusKey, DominoStatuses.DevelopmentStatusKey);
            dict.Add(CSSStatuses.DevelopmentDoneStatusKey, DominoStatuses.DevelopmentDoneStatusKey);
            dict.Add(CSSStatuses.ReviewStatusKey, DominoStatuses.ReviewStatusKey);
            dict.Add(CSSStatuses.ReviewDoneStatusKey, DominoStatuses.ReviewDoneStatusKey);
            dict.Add(CSSStatuses.TestingStatusKey, DominoStatuses.TestingStatusKey);
            dict.Add(CSSStatuses.DoneStatusKey, DominoStatuses.DoneStatusKey);

            return dict[inputStatus];
        }
    }
}